import {createSlice} from '@reduxjs/toolkit'
import type {Slice} from "@reduxjs/toolkit";
import {removeCookieToken, setCookieToken} from "@/utils/auth.ts";

type initialStateType = {
    token: string
}
const initialState: initialStateType = {
    token: ''
}
export const loginSlice: Slice = createSlice({
    name: 'login',
    initialState,
    reducers: {
        setToken: (state, {payload}) => {
            state.token = payload.token
            setCookieToken(payload.token)
        },
        logOut: (state: initialStateType) => {
            state.token = ''
            removeCookieToken()
        }
    }
})
export const {setToken, logOut} = loginSlice.actions
export default loginSlice.reducer
